package com.eclipse.training.swt.helloworld;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

public class ImageRegistry {
	public static final String IMAGE_TAB_2 = "image.tab.2"; //$NON-NLS-1$
	public static final String IMAGE_TAB = "image.tab"; //$NON-NLS-1$
	public static final String IMAGE_SHELL = "image.shell"; //$NON-NLS-1$
	public static final String IMAGE_MENU_EXIT = "image.menu.exit"; //$NON-NLS-1$
	public static final String IMAGE_MENU_OPEN = "image.menu.open"; //$NON-NLS-1$
	public static final String IMAGE_ELEMENT = "image.element"; //$NON-NLS-1$
	public static final String IMAGE_TREE = "image.tree"; //$NON-NLS-1$

	private Map<String, Image> imageRegistry = new HashMap<String, Image>();
	private static ImageRegistry instance;

	protected ImageRegistry() {
	}

	public static ImageRegistry getInstance() {
		if (instance == null)
			instance = new ImageRegistry();
		return instance;
	}

	public void initializeImageRegistry() {
		registerImage(IMAGE_TAB_2, "le-mans.jpg"); //$NON-NLS-1$
		registerImage(IMAGE_TAB, "defaultview_misc.gif"); //$NON-NLS-1$
		registerImage(IMAGE_SHELL, "bkmrk_nav.gif"); //$NON-NLS-1$
		registerImage(IMAGE_MENU_EXIT, "delete.gif"); //$NON-NLS-1$
		registerImage(IMAGE_MENU_OPEN, "editor_area.gif"); //$NON-NLS-1$
		registerImage(IMAGE_ELEMENT, "outline_co.gif"); //$NON-NLS-1$
		registerImage(IMAGE_TREE, "filenav_nav.gif"); //$NON-NLS-1$
	}

	private void registerImage(String key, String fileName) {
		Image image = new Image(Display.getCurrent(), "icons/" + fileName); //$NON-NLS-1$
		imageRegistry.put(key, image);
	}

	public Image getImage(String key) {
		return imageRegistry.get(key);
	}

	public void dispose() {
		Set<String> keys = imageRegistry.keySet();
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next();
			imageRegistry.get(key).dispose();
		}
	}

}
